<template>
  <div>
    <table class="table">
      <caption>
        购物车
      </caption>
      <thead class="thead-dark">
        <tr>
          <th><input v-model="checkbox" type="checkbox" /> <span>全选</span></th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <Shopptr
          v-for="(item, index) in goodList"
          :key="index"
          :goodList="goodList"
          :item="item"
          :index="index"
        ></Shopptr>
        <tr>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td class="tds">合计:</td>
          <td>{{ sum }}</td>
          <td colspan="4"></td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
import Shopptr from '@/components/Day/Day3/Shopp/Shopptr.vue'
export default {
  components: {
    Shopptr
  },
  data() {
    return {
      goodList: [
        {
          name: '诸葛亮',
          price: 1000,
          num: 1,
          checked: false
        },
        {
          name: '蔡文姬',
          price: 1500,
          num: 1,
          checked: false
        },
        {
          name: '妲己',
          price: 2000,
          num: 1,
          checked: false
        },
        {
          name: '鲁班',
          price: 2200,
          num: 1,
          checked: false
        }
      ]
    }
  },
  computed: {
    hesum() {},
    sum() {
      return this.goodList.reduce((acc, val) => {
        if (val.checked) return (acc += val.price * val.num)
        return acc
      }, 0)
    },
    checkbox: {
      set(val) {
        this.goodList.forEach(item => (item.checked = val))
      },
      get() {
        if (this.goodList.length === 0) return false
        return this.goodList.every(item => item.checked)
      }
    }
  },
  methods: {}
}
</script>

<style lang="less" scoped>
* {
  padding: 0;
  margin: 0;
}
.butw {
  width: 30px;
  height: 30px;
  padding: 0;
  margin: 0;
  color: #fff;
  font-size: 20px;
  line-height: 30px !important;
}
th {
  width: 0 !important;
  margin: 0 !important;
  padding: 0 !important;
}
</style>
